package com.leo.foodmaster.admin.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.purchase.entity.PurchaseBill;
import com.leo.foodmaster.admin.purchase.entity.dto.PurchaseBillDTO;
import com.leo.foodmaster.admin.stock.entity.WarehouseInRecord;
import com.leo.foodmaster.admin.stock.entity.WarehouseInRecordDetails;
import com.leo.foodmaster.admin.stock.service.IWarehouseInRecordDetailsService;
import com.leo.foodmaster.admin.stock.service.IWarehouseInRecordService;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 入库记录 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-08-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/stock/warehouseInRecord")
public class WarehouseInRecordController {
    private final IWarehouseInRecordService warehouseInRecordService;
    private final IWarehouseInRecordDetailsService warehouseInRecordDetailsService;

    @ApiOperation("条件分页查询入库记录列表")
    @GetMapping("/list")
    public Result<IPage<WarehouseInRecord>> getWarehouseInRecordPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "仓库Id") @RequestParam(required = false) Long warehouseId,
            @ApiParam(value = "单据编号") @RequestParam(required = false) String serialNum,
            @ApiParam(value = "单据状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime
    ) {
        IPage<WarehouseInRecord> pageList  = warehouseInRecordService
                .getWarehouseInRecordPageList(new Page<>(pageNum, pageSize), warehouseId,
                        serialNum, status, beginTime, endTime);
        return Result.success(pageList);
    }

    @ApiOperation("根据id查询入库记录详情")
    @GetMapping("/{id}")
    public Result<WarehouseInRecord> getById(@ApiParam("入库记录Id") @PathVariable Long id){
        WarehouseInRecord warehouseInRecord = warehouseInRecordService.getById(id);
        List<WarehouseInRecordDetails> list = warehouseInRecordDetailsService.list(Wrappers.lambdaQuery(WarehouseInRecordDetails.class)
                .eq(WarehouseInRecordDetails::getWarehouseInRecordId, id));
        warehouseInRecord.setDetailList(list);
        return Result.success(warehouseInRecord);
    }


    @PostMapping
    @ApiOperation("新增入库记录")
    public Result<Object> addWarehouseInRecord(@RequestBody WarehouseInRecord warehouseInRecord) {
        return warehouseInRecordService.addWarehouseInRecord(warehouseInRecord, "2");
    }

    @PutMapping
    @ApiOperation("修改入库记录")
    public Result<Object> updateWarehouseInRecord(@RequestBody WarehouseInRecord warehouseInRecord) {
        return warehouseInRecordService.updateWarehouseInRecord(warehouseInRecord);
    }

    @PostMapping("/addAuto")
    @ApiOperation("一键入库")
    public Result<Object> autoAddWarehouseInRecord(@RequestBody PurchaseBillDTO purchaseBillDto) {
        return warehouseInRecordService.autoAddWarehouseInRecord(purchaseBillDto);
    }

    @PostMapping("/checkInfoForAutoAdd")
    @ApiOperation("检查采购清单的信息")
    public Result<Object> checkInfoForAutoAdd(@RequestBody PurchaseBill purchaseBill) {
        return warehouseInRecordService.checkInfoForAutoAdd(purchaseBill);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除入库清单")
    public Result<Object> deleteWarehouseInRecord(@ApiParam("入库清单Id") @PathVariable Long id) {
        return warehouseInRecordService.deleteWarehouseInRecord(id);
    }

    @PutMapping("/check")
    @ApiOperation("审核入库记录")
    public Result<Object> checkWarehouseInRecord(@RequestBody WarehouseInRecord warehouseInRecord) {
        return warehouseInRecordService.checkWarehouseInRecord(warehouseInRecord);
    }

}

